package com.CRApp.service.impl;

import com.CRApp.mapper.UserMapper;
import com.CRApp.pojo.User;
import com.CRApp.service.UserService;
import com.CRApp.utils.Md5Util;
import com.CRApp.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @ClassName: UserServiceImpl
 * @Author : LuoFan洛樊
 * @Date : 2025/2/4 - 23:39
 * Description : TODO
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    // 根据用户名查询用户
    @Override
    public User findByUserName(String username) {
        User user = userMapper.findByUserName(username);
        return user;
    }

    // 注册
    @Override
    public void register(User user) {
        // 对密码进行MD5加密
        String md5Password = Md5Util.getMD5String(user.getPassword());
        user.setPassword(md5Password);
        // 设置创建时间
        user.setCreateTime(LocalDateTime.now());
        // 插入用户数据
        userMapper.insert(user);
    }

    // 更新用户信息
    @Override
    public void updateUserInfo(User user) {
        userMapper.updateUserInfo(user);
    }

    // 更新用户头像
    @Override
    public void updateUserPic(String userPic) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateUserPic(id, userPic);
    }

    // 更新用户密码
    @Override
    public void updataPWD(String newPwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        // 对新密码进行MD5加密
        String md5Password = Md5Util.getMD5String(newPwd);
        userMapper.updatePwd(id, md5Password);
    }

}
